package com.neu.dao.ww;

import com.neu.dao.BaseDao;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class GoodsDao extends BaseDao{
    public  List<Map<String,Object>> findByDate(Object...objs){
        List<Map<String,Object>> list=new ArrayList<>();
        String sql="select * from T_GOOD,T_EVENT where T_GOOD.E_ID=T_EVENT.E_ID" +
                " and to_char(T_GOOD.G_DATE,'yyyy-MM-dd')=?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public Map<String,Object> finfById(Object...objs){
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map=null;
        String sql="select * from  V_SHOP_EVENT_KIND" +
                " where g_id=? ";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list!=null&&!list.isEmpty()){
            map=list.get(0);
        }
        return map;
    }
    public List<Map<String,Object>> findByLike(Object...objs){
        List<Map<String,Object>> list=null;
        System.out.println(objs[0]);
        String sql="select  * from T_GOOD,T_EVENT where T_GOOD.E_ID=T_EVENT.E_ID " +
                " and T_GOOD.G_KEY like ?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public List<Map<String,Object>> findByKind(Object...objs){
        List<Map<String,Object>> list=null;
        String sql="select  * from V_SHOP_EVENT_KIND where K_NAME=? ";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public int updateNumById(Object...objs){
        int row=0;
        String sql="update (select * from t_good where g_num>1 and g_id=?) set G_NUM=G_NUM- ? where G_ID=?";
        try {
             row=getQueryRunner().update(sql,objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    public Map<String,Object> findNumById(Object...objs){
        List<Map<String,Object>> list=null;
        Map<String,Object> map=null;
        String sql="select G_NUM from T_GOOD where G_ID=?";
        try {
            list=getQueryRunner().query(sql,new MapListHandler(),objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list!=null&&!list.isEmpty()){
            map=list.get(0);
        }
        return map;
    }

    public int updateAllNumById(List<List<String>> lists) {
        int row=0;
        String sql="update t_good set G_NUM=G_NUM + ? where G_ID=?";
        try {
            row=super.executeBatch(sql,lists);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row;
    }

//    public static void main(String[] args) {
//        GoodsDao goodsDao=new GoodsDao();
//        System.out.println(goodsDao.finfById("63"));
//    }
}
